<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>扩展属性</span>
        <el-button
          @click="showModal('创建')"
          style="float: right; padding: 3px 0"
          type="text"
          >创建</el-button
        >
      </div>

      <el-table :data="ownerAttributes" style="width: 100%">
        <el-table-column prop="id" label="类型"> </el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="value" label="属性"> </el-table-column>

        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              @click="showModal('编辑', scope.row, scope.$index)"
              type="text"
              size="small"
              >编辑</el-button
            >
            <el-button
              @click="deleteListener(scope.$index)"
              type="text"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      :title="attributeTitle"
      :visible.sync="visible"
      width="30%"
      :before-close="closeAndReset"
      append-to-body
    >
      <el-form
        label-width="80px"
        ref="form"
        :model="form"
        :rules="rules"
        :layout="formLayout"
      >
        <el-form-item label="类型" prop="id">
          <el-input v-model="form.id"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="属性值" prop="value">
          <el-input v-model="form.value"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeAndReset">取 消</el-button>
        <el-button type="primary" @click="saveAttribute">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    elementAttributes: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    elementAttributes(newVal) {
      if (newVal) {
        this.ownerAttributes = JSON.parse(
          JSON.stringify(
            (this.elementAttributes[0] && this.elementAttributes[0].values) ||
              []
          )
        );
      }
    },
  },
  data() {
    return {
      visible: false,
      attributeTitle: "",

      attributeList: {},
      attributeIndex: -1,
      attributeForm: {},
      checkNick: false,
      formLayout: "horizontal",
      form: {},
      rules: {
        id: [{ required: false, message: "类型不能为空", trigger: "blur" }],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        value: [{ required: true, message: "属性值不能为空", trigger: "blur" }],
      },
      ownerAttributes: [],
    };
  },
  methods: {
    deleteListener(index) {
      this.ownerAttributes.splice(index, 1);
      //   this.$emit('saved', this.attributeForm)
      this.$emit("change", this.ownerAttributes);
      this.$message.success("删除成功");
    },

    showModal(type, data, index) {
      if (type == "创建") {
        this.attributeTitle = "创建扩展属性";
      } else if (type == "编辑") {
        console.log(type, data, index);
        this.form = data;
        this.attributeTitle = "编辑扩展属性";
        this.attributeIndex = index;
      }

      this.visible = true;
    },
    closeAndReset() {
      this.visible = false;
    },

    saveAttribute() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          //   console.log(this.attributeIndex === -1)
          if (this.attributeIndex === -1) {
            this.ownerAttributes.push(this.form);
            this.$message.success("新增成功");
          } else {
            this.ownerAttributes.splice(this.attributeIndex, 1, this.form);
            this.attributeIndex = -1;
            this.$message.success("保存成功");
          }
          //   this.$emit('saved', this.attributeForm)
          this.$emit("change", this.ownerAttributes);
          this.closeAndReset();
        }
      });
    },
  },
};
</script>
